#include "qcodeplpgenerator.h"
#include "plpgenerator_define.h"
#include "line_analyzer_field.h"

QCodePLPGenerator::QCodePLPGenerator(QEXPPart* part,const QStringList& plp,const QStringList& manu,const QStringList& cat,const QStringList& cat2,const QStringList& cat3,const QStringList& cat4,const QStringList& suppliers,QWidget *parent)
  : QWidget(parent),m_part(part)
{
  ui.setupUi(this);
  m_existingSuppliers = suppliers;
  m_pool = new QObjectPool<QPLPSupplier>(3);
  connect(ui.plp,SIGNAL(textEdited(const QString &)),this,SLOT(plpModified()));
   
  connect(ui.modify,SIGNAL(clicked()),this,SLOT(modifyPLP()));
  connect(ui.add,SIGNAL(clicked()),this,SLOT(addPLP()));
  reset();
  if(m_part)
  {
    if(m_part->get(EXP_CODE_PLP) != EXP_UNKNOWN)
    {
      m_generated = false;
      ui.plp->setText(part->get(EXP_CODE_PLP));
    }
    else
      m_generated = true;

    if(m_part->get(EXP_MANUFACTURER) != EXP_UNKNOWN) 
      ui.manu->setText(part->get(EXP_MANUFACTURER));

    if(m_part->get(EXP_REF_MANUFACTURER) != EXP_UNKNOWN) 
      ui.ref->setText(part->get(EXP_REF_MANUFACTURER));

    ui.plp->setCompleter(new QCompleter(plp));
    ui.manu->setCompleter(new QCompleter(manu));
    ui.cat->setCompleter(new QCompleter(cat));
    ui.cat2->setCompleter(new QCompleter(cat2));
    ui.cat3->setCompleter(new QCompleter(cat3));
    ui.cat4->setCompleter(new QCompleter(cat4));
    
    ui.cat->addItems(cat);
    ui.cat2->addItems(cat2);
    ui.cat3->addItems(cat3);
    ui.cat4->addItems(cat4);

  }
  else
  m_generated = false;
    
  generatePlp();
}

QCodePLPGenerator::~QCodePLPGenerator()
{
  //TODO enlecver les completer
}


void QCodePLPGenerator::generatePlp()
{
  if(m_part)
  {
    QString designator = m_part->get(EXP_DESIGNATOR);
    
    if(designator == EXP_RESISTOR_DESIGNATOR)
    {
      definePackage();
      setCat(LA_RESISTOR_TYPE_1,LA_RESISTOR_TYPE_2,ui.cat3->currentText(),ui.cat4->currentText());
      if(m_generated)
        ui.plp->setText(EXP_RESISTOR_DESIGNATOR + unknown(ui.cat4->currentText()) +"/"+ unknown(m_part->get(EXP_REAL_VALUE)) + "/" +unknown(m_part->get(EXP_TOLERANCE)));
    }
    else if(designator == EXP_CAPACITOR_DESIGNATOR)
    {
      definePackage();
      setCat(LA_CAPACITOR_TYPE_1,LA_CAPACITOR_TYPE_2,ui.cat3->currentText(),ui.cat4->currentText());
      if(m_generated)
        ui.plp->setText(EXP_CAPACITOR_DESIGNATOR + unknown(ui.cat4->currentText()) +"/"+ unknown(m_part->get(EXP_REAL_VALUE)) + "/" +unknown(m_part->get(EXP_TOLERANCE)));
    }
    else if(designator == EXP_SELF_DESIGNATOR)
    {
      definePackage();
      setCat(LA_SELF_TYPE_1,LA_SELF_TYPE_2,ui.cat3->currentText(),ui.cat4->currentText());
      if(m_generated)
        ui.plp->setText(EXP_SELF_DESIGNATOR + unknown(ui.cat4->currentText()) +"/"+ unknown(m_part->get(EXP_REAL_VALUE)) + "/" +unknown(m_part->get(EXP_TOLERANCE)));
    }
  }
}

void QCodePLPGenerator::setCat(QString cat1,QString cat2,QString cat3,QString cat4)
{
  ui.cat->lineEdit()->setText(cat1);
  ui.cat2->lineEdit()->setText(cat2);
  ui.cat3->lineEdit()->setText(cat3);
  ui.cat4->lineEdit()->setText(cat4);
}

void QCodePLPGenerator::reset()
{
  ui.plp->setText("");
  ui.manu->setText("");
  ui.ref->setText("");
  setCat();
  resetSupplier();
}

void QCodePLPGenerator::checkAndEdit(QComboBox  * combo,const QString& text)
{
  if(combo)
  {
    if(combo->findText(text) != -1)
      combo->setCurrentIndex(combo->findText(text));
  }
}


void QCodePLPGenerator::definePackage()
{
  if(m_part)
  {
    if(ui.cat4->currentText().isEmpty() && m_part->get(EXP_PACKAGE) != EXP_UNKNOWN)
    {
      checkAndEdit(ui.cat4,m_part->get(EXP_PACKAGE));
      if(isCMS(ui.cat4->currentText()))
        checkAndEdit(ui.cat3,PLPGEN_CMS);
    }
  }
}

bool QCodePLPGenerator::isCMS(const QString& package) const
{
  return (package == PLPGEN_0603 || package == PLPGEN_0805 || package == PLPGEN_1206 || package == PLPGEN_2512 );
}


void QCodePLPGenerator::plpModified()
{
  m_generated = false;
}

QString QCodePLPGenerator::voidCat(const QString& cat)
{
  if(cat.isEmpty())
    return PLPGEN_VOID;
  else
    return cat;
}

QStringList QCodePLPGenerator::generateLines() const
{
  QStringList line;
  line << formatedToRaw(ui.plp->text());
  line << formatedToRaw(ui.cat->currentText());
  line << formatedToRaw(ui.cat2->currentText());
  line << formatedToRaw(ui.cat3->currentText());
  line << formatedToRaw(ui.cat4->currentText());
  line << formatedToRaw(ui.description->text());
  line << PLPGEN_REPLACE_SYMBOL_SUPPLIER;
  line << PLPGEN_REPLACE_SYMBOL_REF_SUPPLIER;
  line << formatedToRaw(ui.manu->text());
  line << formatedToRaw(ui.ref->text());  
  
  QString templatedLine = line.join(PLPGEN_SEPARATOR);
  QStringList lines;
  
  for(int i=0;i<ui.suppliers->count();i++)
  {
    QPLPSupplier* supplier = (QPLPSupplier*)ui.suppliers->itemAt(i)->widget();
    QString temp = templatedLine;
    temp = temp.replace(PLPGEN_REPLACE_SYMBOL_SUPPLIER,formatedToRaw(supplier->supplier()));
    temp = temp.replace(PLPGEN_REPLACE_SYMBOL_REF_SUPPLIER,formatedToRaw(supplier->ref()));
    lines << temp;
  }
  return  lines;
}


QString QCodePLPGenerator::rawToFormated(QString raw) const
{
  return raw.remove("\"");
}

QString QCodePLPGenerator::formatedToRaw(QString formated) const
{
  return "\"" + rawToFormated(formated) + "\"";
}

void QCodePLPGenerator::resetSupplier()
{
  while(ui.suppliers->count())
  {
    QWidget* widget = ui.suppliers->itemAt(0)->widget();
    widget->disconnect();
    ui.suppliers->removeWidget(widget);
    m_pool->release((QPLPSupplier*)widget);
  }
  addSupplier();
}

void QCodePLPGenerator::deleteSupplier()
{
  QPLPSupplier* supplier = (QPLPSupplier*) sender();
  supplier->disconnect();
  ui.suppliers->removeWidget((QWidget*)supplier);
  ui.suppliers->invalidate();
  m_pool->release((supplier));
  refreshSupplier();
}


void QCodePLPGenerator::addSupplier()
{
  QPLPSupplier* supplier = m_pool->acquire();
  supplier->set(m_existingSuppliers);
  ui.suppliers->addWidget(supplier);
  connect(supplier,SIGNAL(added()),this,SLOT(addSupplier()));
  connect(supplier,SIGNAL(deleted()),this,SLOT(deleteSupplier()));
  refreshSupplier();
}

void QCodePLPGenerator::refreshSupplier()
{
  for(int i=0;i<ui.suppliers->count();i++)
  {
    QPLPSupplier* supplier = (QPLPSupplier*)ui.suppliers->itemAt(i)->widget();
    supplier->setFirst( i==0 );
  }
}


void QCodePLPGenerator::addPLP()
{
  emit addPLPValid();
}

void QCodePLPGenerator::modifyPLP()
{
  emit modifyPLPValid();
}